import { Dialog, Toast } from 'vant'
// 点击发送验证码的时候判断是否合法
export function validatePhone(phone) {
  // 判断输入的手机号是否合法
  if (!phone) {
    Dialog.alert({
      title: '提示',
      message: '手机号码不能为空',
      theme: 'round-button',
      confirmButtonColor: '#328BF2',
      className: 'custom-dialog',
    })
    return false
  } else if (!/^1[3456789]\d{9}$/.test(phone)) {
    Dialog.alert({
      title: '提示',
      message: '请输入正确手机号',
      confirmButtonColor: '#328BF2',
      className: 'custom-dialog',
    })
    return false
  } else {
    return true
  }
}

export function getMessageBox(msg) {
  Dialog.alert({
    title: '提示',
    message: msg,
    theme: 'round-button',
    confirmButtonColor: '#328BF2',
    className: 'custom-dialog',
  })
}
// 点击验证码发送倒计时
export function validateBtn(btnTitle, disabled, thats) {
  // 倒计时
  let time = 60
  let timer = setInterval(() => {
    if (time == 0) {
      clearInterval(timer)
      thats.disabled = false
      thats.btnTitle = '获取验证码'
    } else {
      thats.btnTitle = time + '秒后重试'
      thats.disabled = true
      time--
    }
  }, 1000)
}

// 校验车牌
export function isVehicleNumber(vehicleNumber) {
  var express = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/
  var express1 = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/
  if (express.test(vehicleNumber) == false && express1.test(vehicleNumber) == false) {
    Toast('请输入有效车牌号')
    return false
  } else {
    return true
  }
}
// 转化时间
export function CurentTime(date, timeType) {
  var year = date.getFullYear() // 年
  var month = date.getMonth() + 1 // 月
  var day = date.getDate() // 日
  var hh = date.getHours() // 时
  var mm = date.getMinutes() // 分
  var ss = date.getSeconds()
  var clock = year + '-'
  if (month < 10) { clock += '0' }

  clock += month + '-'

  if (day < 10) { clock += '0' }

  clock += day + ' '

  if (hh < 10) { clock += '0' }
  clock += hh + ':'
  if (mm < 10) clock += '0'
  clock += mm + ':'
  if (ss < 10) clock += '0'
  clock += ss
  return clock
}

export function writeCurrentDate() {
  var now = new Date()
  var year = now.getFullYear() // 得到年份
  var month = now.getMonth()// 得到月份
  var date = now.getDate()// 得到日期
  var day = now.getDay()// 得到周几
  var hour = now.getHours()// 得到小时
  var minu = now.getMinutes()// 得到分钟
  var sec = now.getSeconds()// 得到秒
  var MS = now.getMilliseconds()// 获取毫秒
  var week
  month = month + 1
  if (month < 10) month = '0' + month
  if (date < 10) date = '0' + date
  if (hour < 10) hour = '0' + hour
  if (minu < 10) minu = '0' + minu
  if (sec < 10) sec = '0' + sec
  // if （MS < 100）MS = "0" + MS;
  // var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
  // week = arr_week[day];
  var time = ''
  time = year + '年' + month + '月' + date + '日' // + ' ' + hour + ':' + minu + ':' + sec  + ' ' + week
  // 当前日期赋值给当前日期输入框中（jQuery easyUI）
  return time
  // 设置得到当前日期的函数的执行间隔时间，每1000毫秒刷新一次。
  // var timer = setTimeout('writeCurrentDate()', 1000)
}

// 唤起电话
export function callPhone(phoneNumber) {
  window.location.href = 'tel://' + phoneNumber
}
// 姓名的校验 四个中文系列
export function isFullName(fullname) {
  var regName = /^[\u4e00-\u9fa5]{2,4}$/
  if (!regName.test(fullname)) {
    Dialog.alert({
      message: '姓名填写有误'
    })
    return false
  } else {
    return true
  }
}

/**
 * str.trim || ''
 */
export function trim2Empty(str) {
  if (undefined == str || str == null) {
    return ''
  }
  if (typeof str !== 'string') {
    str = str + ''
  }
  var reg = /^\s+|\s+$/gm
  return str.replace(reg, '')
}

/**
 * true-empty,false-not
 */
export function isEmpty(obj) {
  if (undefined == obj || obj == null) {
    return true
  }
  if (typeof obj === 'number') {
    return isNaN(obj)
  }
  if (typeof obj === 'string') {
    var str = trim2Empty(obj)
    return str == ''
  }
  if (typeof obj === 'function') {
    return false
  }
  if (typeof obj === 'object') {
    for (var k in obj) {
      return false
    }
    return true
  }
  return false
}
export function getUrlParam(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  var r = window.location.search.substr(1).match(reg)
  if (r != null) {
    return unescape(r[2])
  } else {
    return null
  }
}
export function getParam(paramName) {
  var paramValue = '', isFound = !1
  if (window.location.search.indexOf('?') == 0 && window.location.search.indexOf('=') > 1) {
    var arrSource = unescape(window.location.search).substring(1, window.location.search.length).split('&'), i = 0
    while (i < arrSource.length && !isFound) arrSource[i].indexOf('=') > 0 && arrSource[i].split('=')[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split('=')[1], isFound = !0), i++
  }
  return paramValue == '' && (paramValue = null), paramValue
}
